package com.cyan.app.workflow.activity.UserCenter;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.cyan.app.workflow.AppData;
import com.cyan.app.workflow.R;
import com.cyan.app.workflow.base.activity.BaseActivity;
import com.cyan.app.workflow.bean.BaseBean;
import com.cyan.app.workflow.bean.SalaryBean;
import com.cyan.app.workflow.common.HttpConfig;
import com.cyan.app.workflow.customview.PullDownView;
import com.cyan.app.workflow.customview.ShowTipDialog;
import com.cyan.app.workflow.util.AppUtils;
import com.cyan.app.workflow.util.network.GetRequestParams;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 工资列表
 * Created by DXW on 2016/2/27.
 */
public class SalaryActivity  extends BaseActivity  implements PullDownView.OnPullDownListener, AdapterView.OnItemClickListener
 {
    public static final String TAG = "SalaryActivity";
    private List<SalaryBean> mSalaryBeans;
    private SalaryAdapter adapter;
    private ListView prossinfo_lv;
    private PullDownView mPullDownView;
    private Boolean isPressMore = false;
    private int pageCount = 8;
    private Context context = null;
    /** 请求第几页数据 */
    private int intPageNo = 1;
    /** 是否有下一页 */
    private boolean nextPage = false;
     private ShowTipDialog showdialog = new ShowTipDialog();
    private List<SalaryBean> mSalaryDatas = new ArrayList<SalaryBean>();
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_salary);
        titleBar();
        mPullDownView = (PullDownView) findViewById(R.id.user_salary_lv);
        mPullDownView.setOnPullDownListener(this);
        prossinfo_lv = mPullDownView.getListView();
        prossinfo_lv.setOnItemClickListener(this);
        // 隐藏并且禁用头部刷新
        mPullDownView.setHideHeader();
        AsyncLoader task = new AsyncLoader();
        task.execute(AppData.getInstance().getUserId(), "801");
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }
    public class AsyncLoader extends AsyncTask<String, Integer, String> {

         @Override
         protected void onPreExecute() {
             showdialog.showDialog(SalaryActivity.this);
         }

         @Override
         protected String doInBackground(String... params) {
             String result = null;
             try{
                 JSONObject param = new JSONObject();
                 param.put("EmployeeCode", params[0]);
                 result= GetRequestParams.getRequestResult(HttpConfig.HTTP_HR_SERVER_URL, params[1], param.toString(), getApplicationContext());
             }catch (Exception e){
                 Log.e("error", e.toString());
             }
             return result;
         }

         @Override
         protected void onPostExecute(String s) {
             if (s == null) {
                 showdialog.closeDialog();
                 return;
             }
             SalaryLabBean commonBean = new Gson().fromJson(s, SalaryLabBean.class);
             if (commonBean.getCode() < 1) {
                 AppUtils.showTip(SalaryActivity.this, commonBean.getMsg());
             } else {
                 mSalaryBeans = commonBean.getResult();
                 startProssThread();
             }
             showdialog.closeDialog();
         }
     }
    private void getData(int PageNo){
        if(mSalaryBeans==null) return;
        int count = (intPageNo*pageCount)> mSalaryBeans.size()?mSalaryBeans.size():intPageNo*pageCount;
        for(int i = (intPageNo-1)* pageCount;i< count;i++){
            mSalaryDatas.add(mSalaryBeans.get(i));
        }
    }
    private void  showList(){
        // 获取更多数据之外的请求，如首次请求数据，
        if (!isPressMore) {
            getData(intPageNo);
            adapter = new SalaryAdapter(SalaryActivity.this);
            prossinfo_lv.setAdapter(adapter);
        }else {
            // 获取更多数据
            getData(intPageNo);
            adapter.notifyDataSetChanged();
        }
        // 判断是否有下一页
        if (mSalaryBeans.size() > mSalaryDatas.size()) {
            // 显示并启用自动获取更多
            mPullDownView.setShowFooter();
            // 设置可以自动获取更多 滑到最后一个自动获取 改成false将禁用自动获取更多
            mPullDownView.enableAutoFetchMore(true, 1);
        } else {
            // 隐藏 并禁用尾部
            mPullDownView.setHideFooter();
            // 设置可以自动获取更多 滑到最后一个自动获取 改成false将禁用自动获取更多
            mPullDownView.enableAutoFetchMore(false, 1);
        }
        // 告诉它获取更多完毕 这个事线程安全的 可看源代码
        mPullDownView.notifyDidMore();

        isPressMore = false;
    }
    private void titleBar() {
        TextView mTitleName = (TextView) findViewById(R.id.top_title_info);
        mTitleName.setText("工资列表");
        Button back = (Button) findViewById(R.id.top_title_back);
        back.getBackground().setAlpha(255);// 透明度0~255透明度值 ，值越小越透明
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //返回按钮
                finish();
            }
        });
    }
    /**
     * listview单项点击事件
     */
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("salaryDetail", (SalaryBean) (mSalaryBeans.get(position-1)));
        Intent intent = new Intent();
        intent.setClass(SalaryActivity.this, SalaryDetailActivity.class);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    /**
     * listview刷新
     */
    public void onRefresh() {

    }
    /**
     * listview更多
     */
    public void onMore() {
        isPressMore = true;
        // 发送更多请求
        intPageNo++;
        startProssThread();
    }
    // 启动线程
    private void startProssThread() {
        showList();
    }
    public class SalaryAdapter extends BaseAdapter {
        private LayoutInflater inflater = null;
        public SalaryAdapter(Context context) {
            inflater = LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mSalaryDatas.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            holder = new ViewHolder();
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.gravity = Gravity.CENTER_VERTICAL;
            convertView = inflater.inflate(R.layout.user_salary_list, null);
            holder.user_salary_list_yearmonth = (TextView) convertView.findViewById(R.id.user_salary_list_yearmonth);
            holder.user_salary_list_chuqin = (TextView) convertView.findViewById(R.id.user_salary_list_chuqin);
            holder.user_salary_list_yingfa = (TextView) convertView.findViewById(R.id.user_salary_list_yingfa);
            holder.user_salary_list_shifa = (TextView) convertView.findViewById(R.id.user_salary_list_shifa);
            holder.user_salary_list_detail = (ImageView) convertView.findViewById(R.id.user_salary_list_detail);

            holder.user_salary_list_yearmonth.setText(mSalaryDatas.get(position).getYearMonth());
            holder.user_salary_list_chuqin.setText(mSalaryDatas.get(position).getChuQin());
            holder.user_salary_list_yingfa.setText(mSalaryDatas.get(position).getYingFaHeJi());
            holder.user_salary_list_shifa.setText(mSalaryDatas.get(position).getShiFa());
            holder.user_salary_list_detail.setImageResource(R.drawable.right);
            convertView.setTag(holder);
            return convertView;
        }

        class ViewHolder {
            TextView user_salary_list_yearmonth;
            TextView user_salary_list_chuqin;
            TextView user_salary_list_yingfa;
            TextView user_salary_list_shifa;
            ImageView user_salary_list_detail;
        }
    }
     class SalaryLabBean extends  BaseBean{
         private List<SalaryBean> Result;

         public List<SalaryBean> getResult() {
             return Result;
         }

         public void setData(List<SalaryBean> data) {
             Result = data;
         }
     }
 }
